package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.authorizeRFCPanelSubs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.authoriseRFC.GetWorkorderDisplayDataThread;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.RFC;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.StaticGUIDataValues;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.Mainframe;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.AuthorizeRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.authorizeRFCPanelSubs.listeners.CancleDialogButtonListener;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.authorizeRFCPanel.authorizeRFCPanelSubs.listeners.CreateWorkorderActionListener;

public class CreateWorkOrderDialog extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JLabel progress, woID;
	private JComboBox woType, woResponsible;
	private RFC rfc;
	private JButton cancel, createWorkorder;
	private String[] sysadmins;
	private JTextField woAffectedServices, woAffectedInfrastructure, woTitle,
			woPlannedStart, woPlannedEnd, woPlannedEffort;
	private JTextArea woDescription, woNotes;
	
	private AuthorizeRFCPanel authorizeRFCPanel;

	public CreateWorkOrderDialog(Mainframe mainframe, AuthorizeRFCPanel authorizeRFCPanel, RFC rfc) {

		super(mainframe, true);

		this.authorizeRFCPanel = authorizeRFCPanel;
		this.rfc = rfc;
		URL pic = getClass().getResource("/progressbar_cont.gif");
		this.progress = new JLabel(new ImageIcon(pic));
		this.progress.setVisible(true);

		this.setSize(StaticGUIDataValues.JDIALOGDIMENSION);
		this.setLocation(mainframe.getLocation());
		this.setTitle("Workorder erstellen");

		this.add(new LayoutHelper0(), BorderLayout.SOUTH);

		new GetWorkorderDisplayDataThread(this).start();

		this.setVisible(true);
	}

	public AuthorizeRFCPanel getAuthorizeRFCPanel() {
		return authorizeRFCPanel;
	}

	public JTextField getWoTitle() {
		return woTitle;
	}

	public JLabel getProgress() {
		return progress;
	}

	public JComboBox getWoType() {
		return woType;
	}
	
	public JComboBox getWoResponsible() {
		return woResponsible;
	}

	public String[] getSysadmins() {
		return sysadmins;
	}

	public JTextField getWoAffectedServices() {
		return woAffectedServices;
	}

	public JTextField getWoAffectedInfrastructure() {
		return woAffectedInfrastructure;
	}

	public JTextField getWoPlannedStart() {
		return woPlannedStart;
	}

	public JTextField getWoPlannedEnd() {
		return woPlannedEnd;
	}

	public JTextField getWoPlannedEffort() {
		return woPlannedEffort;
	}

	public JTextArea getWoDescription() {
		return woDescription;
	}

	public JTextArea getWoNotes() {
		return woNotes;
	}


	public JLabel getWoID() {
		return woID;
	}

	public RFC getRfc() {
		return rfc;
	}

	public JButton getCancel() {
		return cancel;
	}

	public JButton getCreateWorkorder() {
		return createWorkorder;
	}

	public void setupWorkOrderDiyplay(String[] sysadmins) {
		
		this.sysadmins = sysadmins;
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		
		panel.add(new LayoutHelper2(), BorderLayout.NORTH);
		panel.add(new LayoutHelper4(), BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane(panel);
		
		this.add(scrollPane, BorderLayout.CENTER);
	}
	
	private class LayoutHelper0 extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LayoutHelper0() {
			this.setLayout(new BorderLayout());

			this.add(progress, BorderLayout.NORTH);
			this.add(new LayoutHelper1(), BorderLayout.SOUTH);
		}
	}

	private class LayoutHelper1 extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LayoutHelper1() {
			this.setLayout(new FlowLayout());

			cancel = new JButton("Abbrechen");
			cancel.addActionListener(new CancleDialogButtonListener(
					CreateWorkOrderDialog.this));
			
			createWorkorder = new JButton("Workorder erstellen");
			createWorkorder.setEnabled(false);
			createWorkorder.addActionListener(new CreateWorkorderActionListener(CreateWorkOrderDialog.this));
			
			this.add(cancel);
			this.add(createWorkorder);

		}
	}

	private class LayoutHelper2 extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LayoutHelper2() {
			this.setLayout(new BorderLayout());

			Font font = new Font("", Font.BOLD, 20);
			JLabel title = new JLabel("Workorder erstellen");
			title.setFont(font);
			this.add(title, BorderLayout.NORTH);

			this.add(new LayoutHelper3(), BorderLayout.SOUTH);
		}
	}

	private class LayoutHelper3 extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LayoutHelper3() {
			this.setLayout(new GridLayout(9, 1));

			woID = new JLabel(rfc.getIdRFC() + "_1");
			this.add(new SmallData(new JLabel(" Workorder ID: "), woID));
			
			woTitle = new JTextField(rfc.getSummary());
			this.add(new SmallData(new JLabel(" Titel: "), woTitle));

			String[] types = { "Task", "Test", "Error", "Release" };
			woType = new JComboBox(types);
			this.add(new SmallData(new JLabel(" Work Order Typ: "), woType));

			woResponsible = new JComboBox(sysadmins);
			this.add(new SmallData(new JLabel(" Verantwortlicher Sysadmin: "),
					woResponsible));

			woAffectedServices = new JTextField(rfc.getAffected_Services());
			this.add(new SmallData(new JLabel(" Betroffene Dienste "),
					woAffectedServices));

			woAffectedInfrastructure = new JTextField(
					rfc.getAffected_Infrastructure_Components());
			this.add(new SmallData(new JLabel(" Betroffene Infrastructure: "),
					woAffectedInfrastructure));

			woPlannedStart = new JTextField();
			this.add(new SmallData(new JLabel(" Geplanter Start: "), woPlannedStart));
			
			woPlannedEnd = new JTextField();
			this.add(new SmallData(new JLabel(" Geplanter Abschluss: "), woPlannedEnd));
			
			woPlannedEffort = new JTextField();
			this.add(new SmallData(new JLabel(" Geplante Dauer: "), woPlannedEffort));
		}

	}

	private class SmallData extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private SmallData(JLabel text, JComponent value) {
			this.setLayout(new BorderLayout());
			text.setPreferredSize(new Dimension(150, 10));
			this.add(text, BorderLayout.WEST);
			this.add(value, BorderLayout.CENTER);
		}
	}

	private class LayoutHelper4 extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LayoutHelper4() {
			this.setLayout(new GridLayout(2, 1));

			woDescription = new JTextArea(rfc.getDetailed_Description());
			this.add(new LargeData(new JLabel(" Berschreibung:"),woDescription));
			
			woNotes = new JTextArea();
			this.add(new LargeData(new JLabel(" Anmerkungen: "), woNotes));
		}

	}

	private class LargeData extends JPanel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private LargeData(JLabel text, JComponent component) {
			this.setLayout(new BorderLayout());

			this.add(text, BorderLayout.NORTH);
			this.add(component, BorderLayout.CENTER);
		}
	}
}
